//
//  OSCBaseViewController.m
//  OSClient
//
//  Created by Chaosky on 15/10/19.
//  Copyright © 2015年 1000phone. All rights reserved.
//

#import "OSCBaseViewController.h"

@interface OSCBaseViewController ()

@end

@implementation OSCBaseViewController
{
    NSArray * _subTitles; // 子标题数组
    NSArray * _viewControllers; // 子视图控制器数组
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
// 创建UI
- (void)setupUI
{
    // 当UINavigationController和UIScrollView共同使用时，会出现UIScrollView中的content内容混乱（不显示、contentSize计算错误[就算设置了contentSize也是无效的]）
    // 解决这个问题有两种方式：
    // 1、设置UINavigationController.navigationBar.translucent = NO;
    // 2、添加一个UIView，可以随意大小，但是为了不影响其他视图最好还是设置为1 pt的高度，将其放在其他视图的底层
    
    
    UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 1)];
    [self.view insertSubview:lineView atIndex:0];
    
    self.titleBarView = [[TitleBarView alloc] initWithFrame:CGRectMake(0, 70, self.view.frame.size.width, 40) titles:_subTitles];
    //self.titleBarView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:self.titleBarView];
    
    // 创建水平滚动视图控制器
    _horizontalVC = [[HorizontalViewController alloc] initWithControllers:_viewControllers];
    
    _horizontalVC.view.frame = CGRectMake(0, 110, self.view.frame.size.width, self.view.frame.size.height);
    // 将水平滚动视图控制器作为本视图控制器的子视图控制器
    [self addChildViewController:_horizontalVC];
    // 将子视图控制器上的view添加到本视图控制器上的view
    [self.view addSubview:_horizontalVC.view];
    
    
    // 高内聚，低耦合
    // 联动
    // typeof 获取指定对象的类型，sizeof()关键字
    __weak typeof(self) weakSelf = self;
    self.titleBarView.block = ^(NSInteger index) {
        [weakSelf.horizontalVC scrollToIndex:index];
    };
    self.horizontalVC.block = ^(NSInteger index) {
        [weakSelf.titleBarView scrollToIndex:index];
    };
}

// 实现初始化方法
- (instancetype)initWithTitle:(NSString *)title andSubTitles:(NSArray *)subTitles andViewControllers:(NSArray *)controllers
{
    if (self = [super init]) {
        self.title = title;
        _subTitles = subTitles;
        _viewControllers = controllers;
    }
    return self;
}



@end




